import qrcode
from PIL import Image,ImageDraw,ImageFont
# from matplotlib.image import thumbnail
# import os
# import zipfile
# import matplotlib.pyplot as plt
# def unzip_file(fz_name, path):
#     flag = False
#     if zipfile.is_zipfile(fz_name):  # 检查是否为zip文件
#         with zipfile.ZipFile(fz_name, 'r') as zipf:
#             zipf.extractall(path)
#             # for p in zipf.namelist():
#             #     # 使用cp437对文件名进行解码还原， win下一般使用的是gbk编码
#             #     p = p.encode('cp437').decode('gbk')  # 解决中文乱码
#             #     print(fz_name, p,path)
#             flag = True

#     return {'file_name': fz_name, 'flag': flag}
# unzip_file(fz_name=ipafile,path="/Users/wangshuguan/Desktop/zhengwutong")
def createQR(qrText,iconPath,str):

    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=5,
        border=4,
    )

    # 添加数据
    qr.add_data(qrText)
    # 填充数据
    qr.make(fit=True)
    # 生成图片
    img = qr.make_image(fill_color="green", back_color="white")

    # 添加logo，打开logo照片
    icon = Image.open(iconPath)
    # 获取图片的宽高
    img_w, img_h = img.size
    # 参数设置logo的大小
    factor = 6
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    # 重新设置logo的尺寸
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    # 得到画图的x，y坐标，居中显示
    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    # 黏贴logo照
    img.paste(icon, (w, h), mask=None)

    blankLongImg=Image.new('RGBA',(img_w,img_h+100))
    draw = ImageDraw.Draw(blankLongImg)
    b_size_w,b_size_h = blankLongImg.size
    draw.rectangle((0,0,b_size_w,b_size_h), fill = (255,255,255))
    blankLongImg.paste(img,(0,0))
    fnt = ImageFont.truetype("font.ttc", 30)
    w, h = draw.textsize(str, font=fnt)
    # draw.text((0, img_h), str, fill='black', font=fnt)
        
    draw.text(((img_w-w)/2,img_h+10), str, fill="black", font=fnt)
    return blankLongImg
content = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>items</key>
	<array>
		<dict>
			<key>assets</key>
			<array>
				<dict>
					<key>kind</key>
					<string>software-package</string>
					<key>url</key>
					<string>http://111.7.82.224/kaienwuye-manager.ipa</string>
				</dict>
				<dict>
					<key>kind</key>
					<string>display-image</string>
					<key>url</key>
					<string>https://gitee.com/codeguesser/upload-file/raw/master/kaienwuye.57.png</string>
				</dict>
				<dict>
					<key>kind</key>
					<string>full-size-image</string>
					<key>url</key>
					<string>https://gitee.com/codeguesser/upload-file/raw/master/kaienwuye.512.png</string>
				</dict>
			</array>
			<key>metadata</key>
			<dict>
				<key>bundle-identifier</key>
				<string>com.jianqi.wuye-manager.enterprise</string>
				<key>bundle-version</key>
				<string>1.0.0</string>
				<key>kind</key>
				<string>software</string>
				<key>platform-identifier</key>
				<string>com.apple.platform.iphoneos</string>
				<key>title</key>
				<string>凯恩物业物管</string>
			</dict>
		</dict>
	</array>
</dict>
</plist>'''

def saveImage(path,name):
    img = Image.open(path)
    img.thumbnail((57,57))
    img.save(f"/Users/wangshuguan/Desktop/{name}.57.png")

    img1 = Image.open(path)
    img1.thumbnail((512,512))
    img1.save(f"/Users/wangshuguan/Desktop/{name}.512.png")

# plt.imshow(img)
# plt.show()
##
# zhengwutong文件夹里需要写死文件名称kaienwuye.512.png，这是图片地址
type = 1; #1是业主端，2是物管端
name = "tianjianzc";#文件名称
nameStr = "天健资产";#app名称
plistImagePath = "https://jq.dazhongbanben.com"#https://jq.dazhongbanben.com
ipaPath = "https://jq.dazhongbanben.com"#https://jq.dazhongbanben.com

#显示文字
str = f"{nameStr}(iOS业主)"
if type == 2:
    str = f"{nameStr}(iOS物管)"
#plist文件名称
plistFile = f"{name}.plist"
if type == 2:
    plistFile = f"{name}-manager.plist"
#ipa文件名称
ipaName = f"{name}.ipa"
if type == 2:
    ipaName = f"{name}-manager.ipa"
#qr文件名称
thumbnailName = name
if type == 2:
    thumbnailName = f"{name}-manager"
#包名
packageName = "com.jianqi.wuye.enterprise"
if type == 2:
    packageName = "com.jianqi.wuye-manager.enterprise"




iconPath = "/Users/wangshuguan/Desktop/zhengwutong/kaienwuye.512.png"
# img = createQR(qrText=f"https://jq.dazhongbanben.com/tianianzcandroid.apk",iconPath=iconPath,str=str)
img = createQR(qrText=f"itms-services://?action=download-manifest&url={plistImagePath}/"+plistFile,iconPath=iconPath,str=str)
img.save(f"/Users/wangshuguan/Desktop/{thumbnailName}.qr.png")
saveImage(iconPath,thumbnailName)
dic = {
    "kaienwuye-manager.ipa":ipaName,
    "kaienwuye.57.png":f"{thumbnailName}.57.png",
    "kaienwuye.512.png":f"{thumbnailName}.512.png",
    "凯恩物业物管":str,
    "com.jianqi.wuye-manager.enterprise":packageName,
    "https://gitee.com/codeguesser/upload-file/raw/master":plistImagePath,
    "http://111.7.82.224":ipaPath
}
for x in dic:
    content = content.replace(x,dic[x])

with open("/Users/wangshuguan/Desktop/" + plistFile, 'w+',encoding='utf-8') as f:
    f.write(content)
